package vn.anhqn.englishsentensetranslator.SentenseLookup;

import vn.anhqn.englishsentensetranslator.R;
import vn.anhqn.englishsentensetranslator.SentenseLookup.DialogConfirmDelete.confirmDeleteListener;
import vn.anhqn.englishsentensetranslator.addsentense.AddSentenseFragment;
import vn.anhqn.englishsentensetranslator.base.fragment.FragmentBackAction;
import vn.anhqn.englishsentensetranslator.constant.Constant;
import vn.anhqn.englishsentensetranslator.model.Sentense;
import vn.anhqn.englishsentensetranslator.utils.SqliteHelper;
import vn.anhqn.englishsentensetranslator.utils.Utils;
import vn.anhqn.englishsentensetranslator.widget.HeverticalTextView;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class SentenseDetail extends FragmentBackAction implements OnLongClickListener {

	private Sentense sentense;
	private HeverticalTextView tvFromLang;
	private HeverticalTextView tvToLang;
	private SqliteHelper sqliteHelper;
	private Button btnDel;
	private Button btnEdit;
	@Override
	public void setBerhaviorView(View mView) {
		sqliteHelper = new SqliteHelper(getActivity());
		tvFromLang = (HeverticalTextView) mView.findViewById(R.id.tv_from_lang);
		tvFromLang.setOnLongClickListener(this);
		tvToLang = (HeverticalTextView) mView.findViewById(R.id.tv_to_lang);
		tvToLang.setOnLongClickListener(this);
		
		btnDel = (Button) mView.findViewById(R.id.btn_delete);
		btnDel.setOnClickListener(this);
		Utils.getIntance().setFontFromAsset(btnDel, "Helvetica/helvetica-normal.ttf");
		btnEdit = (Button) mView.findViewById(R.id.btn_edit);
		btnEdit.setOnClickListener(this);
		Utils.getIntance().setFontFromAsset(btnEdit, "Helvetica/helvetica-normal.ttf");
		getFirstData();
	}

	private void getFirstData() {
		if (getArguments() != null) {
			sentense = (Sentense) getArguments().getSerializable(Constant.KEY_EXTRA_SENTENSE);
			tvFromLang.setText(sentense.getFromLang());
			tvToLang.setText(sentense.getToLang());
			setTitleHeader(sentense.getFromLang());
		}
	}

	@Override
	public int getResLayout() {
		return R.layout.sentense_detail_fragment;
	}

	@Override
	public int getidTitle() {
		return 0;
	}

	@Override
	public void onClickView(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btn_delete:
			DialogConfirmDelete dialogConfirm = new DialogConfirmDelete(getActivity());
			dialogConfirm.setListener(new confirmDeleteListener() {
				@Override
				public void onOK() {
						sqliteHelper.deleteSentenseEtoY(sentense);
					finishThis();
				}
			});
			dialogConfirm.show();
			break;
		case R.id.btn_edit:
			Bundle bundle = new Bundle();
			bundle.putBoolean(Constant.KEY_EXTRA_IS_FINISH, true);
			bundle.putString(Constant.KEY_EXTRA_FROM_TEXT, tvFromLang.getText().toString());
			bundle.putString(Constant.KEY_EXTRA_TO_TEXT, tvToLang.getText().toString());
			bundle.putInt(Constant.KEY_EXTRA_SENTENSE_ACTION,Constant.EDIT);
			bundle.putInt(Constant.KEY_EXTRA_SENTENSE_ID,sentense.getId());
			bundle.putInt(Constant.KEY_EXTRA_CAT_ID, sentense.getIdCategory());
			replaceNewInstance(AddSentenseFragment.class, bundle);
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onLongClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.tv_from_lang:
			Utils.getIntance().copyTextToClipboad(getActivity(), tvFromLang.getText().toString(), R.string.copy_messege);
			break;
		case R.id.tv_to_lang:
			Utils.getIntance().copyTextToClipboad(getActivity(), tvToLang.getText().toString(), R.string.copy_messege);
			break;
		default:
			break;
		}
		return false;
	}
}
